/*
*
* Copyright (c) 2011 Vaulting Systems International
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
* of the Software, and to permit persons to whom the Software is furnished to do  
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all  
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE  
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.ekeymanlib.business;

import java.util.ArrayList;
import java.util.List;

import com.ekeymanlib.dao.AccessAuditDao;
import com.ekeymanlib.dao.AppDeviceDao;
import com.ekeymanlib.dao.VendorDao;
import com.ekeymanlib.domain.AccessAudit;
import com.ekeymanlib.domain.AppDevice;
import com.ekeymanlib.domain.Vendor;
import com.ekeymanlib.dto.AppDeviceApiKeys;
import com.ekeymanlib.dto.AppDeviceGrid;
import com.ekeymanlib.dto.Application;
import com.ekeymanlib.dto.SearchFilter;
import com.ekeymanlib.util.KeyUtils;

public class AppDeviceBO {

	private VendorDao vendorDao;
	private AppDeviceDao appDeviceDao;
	private AccessAuditDao accessAuditDao;
	
	public AppDeviceBO(){}

	public AppDeviceApiKeys createAppDevice(String username, String application, String device){
		
		AppDeviceApiKeys appDeviceApiKeys = new AppDeviceApiKeys();
		
		Vendor vendor = getVendorDao().getVendor(username);
		if(vendor != null){
			AppDevice appDevice = new AppDevice();
			appDevice.setVendor(vendor);
			appDevice.setApplication(application);
			appDevice.setDevice(device);
			appDevice.setPublicKey(KeyUtils.generateAppDeviceKey());
			appDevice.setPrivateKey(KeyUtils.generateAppDeviceKey());
			
			getAppDeviceDao().saveAppDevice(appDevice);
			
			populateAppDeviceApiKeys(appDevice, appDeviceApiKeys);
		}
	
		return appDeviceApiKeys;
	}
	
	public AppDeviceApiKeys registerAppDevice(String apiKey, String application, String device){
		
		AppDeviceApiKeys appDeviceApiKeys = new AppDeviceApiKeys();
		
		Vendor vendor = getVendorDao().getVendor(apiKey);
		if(vendor != null){
			
			// Need to return existing app device if already registered!
			AppDevice appDevice = getAppDeviceDao().getAppDevice(apiKey, application, device);
			
			if(appDevice == null){
				appDevice = new AppDevice();
				appDevice.setVendor(vendor);
				appDevice.setApplication(application);
				appDevice.setDevice(device);
				appDevice.setPublicKey(KeyUtils.generateAppDeviceKey());
				appDevice.setPrivateKey(KeyUtils.generateAppDeviceKey());
				
				getAppDeviceDao().saveAppDevice(appDevice);
			}
			
			populateAppDeviceApiKeys(appDevice, appDeviceApiKeys);
		}
	
		return appDeviceApiKeys;
	}
	
	public AppDeviceApiKeys getAppDevice(String publicKey){
		
		AppDeviceApiKeys appDeviceApiKeys = new AppDeviceApiKeys();
		AppDevice appDevice = getAppDeviceDao().getAppDevice(publicKey);
		
		if(appDevice != null){
			populateAppDeviceApiKeys(appDevice, appDeviceApiKeys);
		}
		
		return appDeviceApiKeys;
	}
	
	public AppDeviceApiKeys generateNewPrivateKey(String publicKey, String apiKey){
		
		AppDeviceApiKeys appDeviceApiKeys = new AppDeviceApiKeys();
		
		Vendor vendor = getVendorDao().getVendor(apiKey);
		if(vendor != null){
			
			AppDevice appDevice = getAppDeviceDao().getAppDevice(publicKey);
			
			if(appDevice != null){
				appDevice.setPrivateKey(KeyUtils.generateAppDeviceKey());
				getAppDeviceDao().saveAppDevice(appDevice);
				
				populateAppDeviceApiKeys(appDevice, appDeviceApiKeys);
			}
		}
	
		return appDeviceApiKeys;
	}
	
	public void deleteAppDevice(String publicKey){
		
			getAppDeviceDao().deleteAppDevice(publicKey);
	}
	
	public void deleteAppDeviceById(long id, String apiKey){
		
		AppDevice appDevice = getAppDeviceDao().getAppDeviceById(id);
		
		if(appDevice != null){
			AccessAudit accessAudit = new AccessAudit();
			accessAudit.setAccessResult(AccessAudit.SUCCESS);
			accessAudit.setAccessType("delete app device");
			accessAudit.setApplication(appDevice.getApplication());
			accessAudit.setDevice(appDevice.getDevice());
			accessAudit.setResourceUri("");
			accessAudit.setLocation("Administration website");

			getAppDeviceDao().deleteAppDeviceById(id, apiKey);
			
			getAccessAuditDao().saveAccessAudit(accessAudit);
		}
	}

	public List<Application> listApplications(String apiKey){
		return getAppDeviceDao().listApplications(apiKey);
	}
	
	public long getAppDeviceCount(List<SearchFilter> searchFilters){
		return getAppDeviceDao().getAppDeviceCount(searchFilters);
	}
	
	public List<AppDeviceGrid> findAppDevices(List<SearchFilter> searchFilters, 
			String sortIndex, String sortOrder, int firstResult, int maxResults){

		List<AppDevice> appDevices = getAppDeviceDao().findAppDevices(searchFilters, sortIndex, sortOrder, firstResult, maxResults);	
		return gridTransformation(appDevices);
	}

	
	private void populateAppDeviceApiKeys(AppDevice appDevice, AppDeviceApiKeys appDeviceApiKeys){
		appDeviceApiKeys.setPublicKey(appDevice.getPublicKey());
		appDeviceApiKeys.setPrivateKey(appDevice.getPrivateKey());
	}
	
	private List<AppDeviceGrid> gridTransformation(List<AppDevice> appDevices){
		
		List<AppDeviceGrid> gridAppDevices = new ArrayList<AppDeviceGrid>();
		
		for(AppDevice ad : appDevices){
			AppDeviceGrid adg = new AppDeviceGrid();
			adg.setId(ad.getId());
			adg.setApplication(ad.getApplication());
			adg.setDevice(ad.getDevice());
			adg.setPublicKey(ad.getPublicKey());
			adg.setPrivateKey(ad.getPrivateKey());
			gridAppDevices.add(adg);
		}
		
		return gridAppDevices;
	}
	


	
	public VendorDao getVendorDao() {
		return vendorDao;
	}

	public void setVendorDao(VendorDao vendorDao) {
		this.vendorDao = vendorDao;
	}

	public void setAppDeviceDao(AppDeviceDao appDeviceDao) {
		this.appDeviceDao = appDeviceDao;
	}

	public AppDeviceDao getAppDeviceDao() {
		return appDeviceDao;
	}

	public void setAccessAuditDao(AccessAuditDao accessAuditDao) {
		this.accessAuditDao = accessAuditDao;
	}

	public AccessAuditDao getAccessAuditDao() {
		return accessAuditDao;
	}
}
